package com.xzssmk.leisure.x.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzssmk.leisure.x.mapper.CardTypeMapper;
import com.xzssmk.leisure.x.po.CardType;
import com.xzssmk.leisure.x.po.CardTypeExample;
import com.xzssmk.leisure.x.po.CardTypeSelect;
import com.xzssmk.leisure.x.service.BaseService;
import com.xzssmk.leisure.x.service.CardTypeService;
import com.xzssmk.leisure.x.util.DataTables;
import com.xzssmk.leisure.x.util.RequestStatusUtil;

@Service
public class CardTypeServiceImpl extends BaseService implements CardTypeService {

    @Autowired
    private CardTypeMapper cardTypeMapper;

    @Override
    public Map<String, Object> add(CardType cardType) {

        cardType.setTypeSymbol(cardType.getTypeSymbol().toUpperCase());
        cardType.setCreateTime(new Date());
        cardType.setModifyTime(new Date());

        Map<String, Object> map = new HashMap<>();

        CardTypeExample example = new CardTypeExample();
        example.createCriteria().andTypeSymbolEqualTo(cardType.getTypeSymbol());
        List<CardType> list = cardTypeMapper.selectByExample(example);
        if (null == list || list.size() == 0) {
            if (cardTypeMapper.insert(cardType) > 0) {
                map.put("status", RequestStatusUtil.SUCCESS);
            } else {
                map.put("status", RequestStatusUtil.FAILURE);
            }
            return map;
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
            return map;
        }
    }

    @Override
    public Map<String, Object> delete(List<Integer> idlist) {
        Map<String, Object> map = new HashMap<>();
        if (cardTypeMapper.deleteByPrimaryKeys(idlist) > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public Map<String, Object> update(CardType cardType) {
        cardType.setModifyTime(new Date());
        Map<String, Object> map = new HashMap<>();

        CardTypeExample example = new CardTypeExample();
        example.createCriteria().andCardTypeIdEqualTo(cardType.getCardTypeId())
            .andTypeSymbolEqualTo(cardType.getTypeSymbol());
        List<CardType> list = cardTypeMapper.selectByExample(example);
        if (null == list || list.size() == 0) {
            map.put("status", RequestStatusUtil.FAILURE);
            return map;
        } else {
            if (Integer.parseInt(cardType.getCardinalNumber()) < Integer.parseInt(list.get(0).getCardinalNumber())) {
                map.put("status", RequestStatusUtil.FAILURE);
                return map;
            }
        }

        if (cardTypeMapper.updateByPrimaryKeySelective(cardType) > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("modify_time desc");
        // https: // blog.csdn.net/lvbang_lzt/article/details/54375091
        // https: // blog.csdn.net/evane1890/article/details/63262610
        // https: // blog.csdn.net/qq_33315102/article/details/80572566
        if (!StringUtils.isEmpty(dataTables.getColumn())) {
            String column = propertyToColumn(CardTypeMapper.class, dataTables.getColumn());
            if (!StringUtils.isEmpty(column)) {
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }
        List<CardType> listCardType = cardTypeMapper.getPageList(dataTables.getSubSQL());
        PageInfo<CardType> pageInfo = new PageInfo<CardType>(listCardType);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());

        return dataTables;
    }

    @Override
    public CardType getById(Integer id) {
        return cardTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CardTypeSelect> getCardTypeSelect() {
        return cardTypeMapper.getCardTypeSelect();
    }

}
